/**
 * Created by jiaoshou on 2016/3/20.
 */
//获取innerText
/**
 *
 * @param element
 * @returns {*}
 */
function getInnerText(element) {
    //能力检测
    return element.textContent ? element.textContent : element.innerText;
}
//设置innerText
/**
 *
 * @param element
 * @param content
 */
function setInnerText(element, content) {
    if (typeof element.textContent === "string") {
        element.textContent = content;
    } else {
        element.innerText = content;
    }
}

//兼容浏览器
//获取下一个紧邻的兄弟元素
function getNextElement(element) {
    //能力检测
    if (element.nextElementSibling) {
        return element.nextElementSibling;
    } else {
        var node = element.nextSibling;
        while (node && node.nodeType !== 1) {
            node = node.nextSibling;
            break;
        }
        return node;
    }
}

/**
 * 返回上一个元素
 * @param element
 * @returns {*}
 */
function getPreviousElement(element) {
    if (element.previousElementSibling) {
        return element.previousElementSibling;
    } else {
        var el = element.previousSibling;
        while (el && el.nodeType !== 1) {
            el = el.previousSibling;
        }
        return el;
    }
}

/**
 * 返回第一个元素 firstElementChild的浏览器兼容
 * @param parent
 * @returns {*}
 */
function getFirstElement(parent) {
    if (parent.firstElementChild) {
        return parent.firstElementChild;
    } else {
        var el = parent.firstChild;
        while (el && el.nodeType !== 1) {
            el = el.nextSibling;
        }
        return el;
    }
}

/**
 * 返回最后一个元素
 * @param parent
 * @returns {*}
 */
function getLastElement(parent) {
    if (parent.lastElementChild) {
        return parent.lastElementChild;
    } else {
        var el = parent.lastChild;
        while (el && el.nodeType !== 1) {
            el = el.previousSibling;
        }
        return el;
    }
}


/**
 * 获取当前元素的所有兄弟元素
 * @param element
 * @returns {Array}
 */
function siblings(element) {
    if (!element) return;

    var elements = [];
    //前面的兄弟节点
    var el = element.previousSibling;
    while (el) {
        if (el.nodeType === 1) {
            elements.push(el);
        }
        el = el.previousSibling;
    }

    //后面的兄弟节点
    el = element.nextSibling;
    while (el) {
        if (el.nodeType === 1) {
            elements.push(el);
        }
        el = el.nextSibling;
    }
    return elements;
}

function my$(id) {
    return document.getElementById(id);
}
/**
 * 把日期对象转换成日期字符串
 * @param date
 * @returns {string}
 */
function getTimeString(date) {
    //年
    var year = date.getFullYear();
    //月
    var month = date.getMonth() + 1; //月份从0开始
    //日
    var day = date.getDate();
    //小时
    var hour = date.getHours();
    //分钟
    var minute = date.getMinutes();
    //秒
    var second = date.getSeconds();

    //如果不够两位补0
    month = month < 10 ? "0" + month : month;
    day = day < 10 ? "0" + day : day;
    hour = hour < 10 ? "0" + hour : hour;
    minute = minute < 10 ? "0" + minute : minute;
    second = second < 10 ? "0" + second : second;

    return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
}

//处理事件的一组方法
var EventTools = {
    addEventListener: function (element, eventName, listener) {
        //能力检测
        if (element.addEventListener) {
            element.addEventListener(eventName, listener, false);
        } else if (element.attachEvent) {
            element.attachEvent("on" + eventName, listener);
        } else {
            element["on" + eventName] = listener;
        }
    },
    removeEventListener: function (element, eventName, listener) {
        if (element.removeEventListener) {
            element.removeEventListener(eventName, listener, false);
        } else if (element.detachEvent) {
            element.detachEvent("on" + eventName, listener);
        } else {
            element["on" + eventName] = null;
        }
    },
    //获取事件对象  兼容浏览器
    getEvent: function (e) {
        return e || window.event;
    },
    //兼容target
    getTarget: function (e) {
        return e.target || e.srcElement;
    },
    getPageX: function (e) {
        //能力检测
        if (e.pageX) {
            return e.pageX;
        } else {

            //获取文档的根元素 -html
            //document.documentElement.scrollTop
            //document.body.scrollTop
            var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
            return e.clientX + scrollLeft;
        }
    },
    getPageY: function (e) {
        if (e.pageY) {
            return e.pageY;
        } else {
            var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
            return e.clientY + scrollTop;
        }
    }
};